#include "StdAfx.h"
#include "security_identifier.h"

using namespace std;

//-------------------------------------------------------------------------------
security_identifier::security_identifier( const SID& sid ) : _sid(sid)
{
}
//-------------------------------------------------------------------------------
security_identifier::security_identifier( const CSid& sid ) : _sid(sid)
{
}
//-------------------------------------------------------------------------------
security_identifier::security_identifier( const std::wstring& account_name, const std::wstring& computer_name /*= L""*/ )
{
	set(account_name, computer_name);
}
//-------------------------------------------------------------------------------
security_identifier::security_identifier( const std::wstring& string_sid )
{
	set(string_sid);
}
//-------------------------------------------------------------------------------
bool security_identifier::set( const SID& sid )
{
	return _sid.LoadAccount(&sid);
}
//-------------------------------------------------------------------------------
bool security_identifier::set( const CSid& sid )
{
	_sid = sid;
	return true;
}
//-------------------------------------------------------------------------------
bool security_identifier::set( const std::wstring& string_sid )
{
	PSID outsid = NULL;
	if(!::ConvertStringSidToSid(string_sid.c_str(), &outsid)){
		return false;
	}

	if(!_sid.LoadAccount((SID*)outsid))
	{
		::LocalFree(outsid);
		return false;
	}

	::LocalFree(outsid);

	return true;
}
//-------------------------------------------------------------------------------
bool security_identifier::set( const std::wstring& account_name, const std::wstring& computer_name )
{
	return _sid.LoadAccount(account_name.c_str(), computer_name.empty() ? NULL : computer_name.c_str());
}
//-------------------------------------------------------------------------------
wstring security_identifier::get_account_name(void) const
{
	return _sid.AccountName();
}
//-------------------------------------------------------------------------------
wstring security_identifier::get_domain_name(void) const
{
	return _sid.Domain();
}
//-------------------------------------------------------------------------------
wstring security_identifier::get_full_name(void) const
{
	wstring res = account_name+L"\\";
	res += domain_name;
	return res;
}
//-------------------------------------------------------------------------------
wstring security_identifier::get_as_string(void) const
{ 
	return _sid.Sid();
}
//-------------------------------------------------------------------------------
const SID* security_identifier::get_sid(void) const
{
	return (const SID*)_sid.GetPSID();
}
//-------------------------------------------------------------------------------
bool security_identifier::get_is_valid(void) const
{
	return _sid.IsValid();
}
//-------------------------------------------------------------------------------
security_identifier::operator const SID&()
{
	return (const SID&)_sid;
}
//-------------------------------------------------------------------------------
security_identifier::operator SID*()
{
	return (SID*)_sid.GetPSID();
}
//-------------------------------------------------------------------------------
security_identifier::operator ATL::CSid&()
{
	return _sid;
}
//-------------------------------------------------------------------------------
SID_NAME_USE security_identifier::get_type(void) const
{
	return _sid.SidNameUse();
}
//-------------------------------------------------------------------------------
security_identifier security_identifier::create_well_known( WELL_KNOWN_SID_TYPE sid_type, PSID domain /*= NULL*/ )
{
	PSID sid = NULL;
	DWORD bufsize = SECURITY_MAX_SID_SIZE;
	::CreateWellKnownSid(sid_type, domain, sid, &bufsize);

	CSid res((const SID*)sid, NULL);

	::LocalFree(sid);

	return res;
}
//-------------------------------------------------------------------------------